package ltd.newbee.mall.dao;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import ltd.newbee.mall.entity.MallUserPointConsume;
import ltd.newbee.mall.entity.MallUserPointGrant;

/**
 * <pre>
 * 用户积分方法
 * </pre>
 * @author rusth
 * @version $$ id: object.java ,v 0.1 2024/6/19 09:46 rusth Exp $$
 */
public interface MallUserPointMapper {

    // 发放记录
    @Insert("INSERT INTO `tb_newbee_mall_user_point_grant` (`grant_id`, `user_id`, `point`, `create_time`, `update_time`, `is_deleted`, `create_by`, `update_by`, `start_time`, `end_time`) VALUES (#{grantId}, #{userId}, #{point}, #{createTime}, #{updateTime}, #{isDeleted}, #{createBy}, #{updateBy}, #{startTime}, #{endTime})")
    int insertGrant(MallUserPointGrant grant);

    // 删除发放记录
    @Update("update tb_newbee_mall_user_point_grant set is_deleted=1,update_time=now(),update_by=#{updateBy} where grant_id=#{grantId}")
    void deleteGrant(@Param("grantId") Long grandId, @Param("updateBy") Long updateBy);

    @Select("select * from tb_newbee_mall_user_point_grant where grant_id=#{grantId}")
    @Results({ @Result(property = "grantId", column = "grant_id"), @Result(property = "userId", column = "user_id"), @Result(property = "isDeleted", column = "is_deleted"),
            @Result(property = "createBy", column = "create_by"), @Result(property = "point", column = "point"), @Result(property = "updateBy", column = "update_by"),
            @Result(property = "createTime", column = "create_time"), @Result(property = "updateTime", column = "update_time"), @Result(property = "startTime", column = "start_time"),
            @Result(property = "endTime", column = "end_time") })
    MallUserPointGrant getGrantById(@Param("grantId") Long grantId);

    // (后管使用忽略时间)总共多次发放 and start_time<NOW() and end_time>now()
    @Select("select IFNULL(sum(point),0.0000) from tb_newbee_mall_user_point_grant where user_id=#{userId} and is_deleted=0 and start_time<=NOW() and end_time>NOW()")
    BigDecimal getGrantSum(@Param("userId") Long userId);

    // (后管使用)总共消费多少
    @Select("select IFNULL(sum(point),0.0000) from tb_newbee_mall_user_point_consume where user_id=#{userId} and is_deleted=0")
    BigDecimal getConsumeSum(@Param("userId") Long userId);

    @Update("update tb_newbee_mall_user_point_consume set is_deleted=#{isDeleted} where grant_id=#{grantId}")
    void deleteConsume(@Param("grantId") Long grandId, @Param("isDeleted") Integer isDeleted);

    @Select("select IFNULL(sum(point),0.0000) from tb_newbee_mall_user_point_consume where grant_id=#{grantId} and is_deleted=0")
    BigDecimal getUserConsumeSum(@Param("grantId") Long grantId);

    @Select("select * from tb_newbee_mall_user_point_grant where user_id=#{userId} and is_deleted=0 and start_time<=NOW() and end_time>NOW()")
    @Results({ @Result(property = "grantId", column = "grant_id"), @Result(property = "userId", column = "user_id"), @Result(property = "isDeleted", column = "is_deleted"),
            @Result(property = "createBy", column = "create_by"), @Result(property = "point", column = "point"), @Result(property = "updateBy", column = "update_by"),
            @Result(property = "createTime", column = "create_time"), @Result(property = "updateTime", column = "update_time"), @Result(property = "startTime", column = "start_time"),
            @Result(property = "endTime", column = "end_time") })
    List<MallUserPointGrant> getUserAllGrant(@Param("userId") Long userId);

    @Insert("INSERT INTO `newbee_mall`.`tb_newbee_mall_user_point_consume` (`consume_id`, `point`, `grant_id`, `order_no`, `user_id`, `create_time`, `update_time`, `is_deleted`) VALUES (#{consumeId}, #{point}, #{grantId}, #{orderNo}, #{userId}, #{createTime}, #{updateTime}, #{isDeleted})")
    int insertConsume(MallUserPointConsume consume);

    @Select("select * from tb_newbee_mall_user_point_grant where end_time <= #{expireTime} and is_deleted=0")
    @Results({ @Result(property = "grantId", column = "grant_id"), @Result(property = "userId", column = "user_id"), @Result(property = "isDeleted", column = "is_deleted"),
            @Result(property = "createBy", column = "create_by"), @Result(property = "point", column = "point"), @Result(property = "updateBy", column = "update_by"),
            @Result(property = "createTime", column = "create_time"), @Result(property = "updateTime", column = "update_time"), @Result(property = "startTime", column = "start_time"),
            @Result(property = "endTime", column = "end_time") })
    List<MallUserPointGrant> queryExpireGrant(@Param("expireTime") Date expireTime);

    @Update("update tb_newbee_mall_user_point_grant set is_deleted=3 where is_deleted=0 and end_time <= #{expireTime}")
    int expireGrant(@Param("expireTime") Date expireTime);

    @Update("update tb_newbee_mall_user_point_consume set is_deleted=3 where is_deleted=0 and grant_id=#{grantId}")
    int expireConsume(@Param("grantId") Long grantId);

    @Select("select * from tb_newbee_mall_user_point_grant  order by update_time desc limit #{start},#{limit}")
    @Results({ @Result(property = "grantId", column = "grant_id"), @Result(property = "userId", column = "user_id"), @Result(property = "isDeleted", column = "is_deleted"),
            @Result(property = "createBy", column = "create_by"), @Result(property = "point", column = "point"), @Result(property = "updateBy", column = "update_by"),
            @Result(property = "createTime", column = "create_time"), @Result(property = "updateTime", column = "update_time"), @Result(property = "startTime", column = "start_time"),
            @Result(property = "endTime", column = "end_time") })
    List<MallUserPointGrant> getGrantListByAdmin(@Param("start") Integer start, @Param("limit") Integer limit);

    @Select("select IFNULL(sum(point),0.0000) from tb_newbee_mall_user_point_consume where grant_id=#{grantId}")
    BigDecimal getGrantConsumeSum(@Param("grantId") Long grantId);

    @Select("select count(1) from tb_newbee_mall_user_point_grant")
    int countGrantListByAdmin();

}
